package cn.wolf.tools;

import java.util.HashMap;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

public class AlertHelper {

	private static HashMap<String, Dialog> dialogs = new HashMap<String, Dialog>();

	public static AlertDialog showConfirm(Context context, String sMessage,
			DialogInterface.OnClickListener confirmClick,
			DialogInterface.OnClickListener cancelClick) {
		dismissAlert(context);
		AlertDialog alertDlg = new AlertDialog.Builder(context).create();
		dialogs.put(context.toString(), alertDlg);
		alertDlg.setTitle("提示");
		alertDlg.setMessage(sMessage);
		alertDlg.setButton("确定", confirmClick);
		alertDlg.setButton2("取消", cancelClick);
		alertDlg.setCanceledOnTouchOutside(false);
		alertDlg.show();
		return alertDlg;
	}

	/**
	 * 自定义对话框
	 * @param context
	 * @param title 标题
	 * @param sMessage 内容
	 * @param confirmText 确定按钮文字
	 * @param cancelText 取消按钮文字
	 * @param confirmClick 确定点击响应
	 * @param cancelClick 取消点击响应
	 * @return AlertDialog
	 */
	public static AlertDialog showConfirm(Context context, String title, String sMessage,
			String confirmText, String cancelText, DialogInterface.OnClickListener confirmClick,
			DialogInterface.OnClickListener cancelClick) {
		dismissAlert(context);
		AlertDialog alertDlg = new AlertDialog.Builder(context).create();
		dialogs.put(context.toString(), alertDlg);
		alertDlg.setTitle(title);
		alertDlg.setMessage(sMessage);
		alertDlg.setButton(confirmText, confirmClick);
		alertDlg.setButton2(cancelText, cancelClick);
		alertDlg.show();
		return alertDlg;
	}

	public static AlertDialog showMessage(Context context, String sMessage) {
		return showMessage(context, sMessage, null);
	}

	public static AlertDialog showMessage(Context context, String sMessage,
			DialogInterface.OnClickListener listener) {
		dismissAlert(context);
		AlertDialog alertDlg = new AlertDialog.Builder(context).create();
		dialogs.put(context.toString(), alertDlg);
		alertDlg.setTitle("提示");
		alertDlg.setMessage(sMessage);
		DialogInterface.OnClickListener click = null;
		if (listener != null) {
			click = listener;
		} else {
			click = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface iDlg, int which) {
					iDlg.cancel();
				}
			};
		}
		alertDlg.setButton("确定", click);

		alertDlg.show();
		return alertDlg;
	}

	public static AlertDialog showMessage(Context context, String title, String sMessage,
			DialogInterface.OnClickListener listener) {
		dismissAlert(context);
		AlertDialog alertDlg = new AlertDialog.Builder(context).create();
		dialogs.put(context.toString(), alertDlg);
		alertDlg.setTitle(title);
		alertDlg.setMessage(sMessage);
		DialogInterface.OnClickListener click = null;
		if (listener != null) {
			click = listener;
		} else {
			click = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface iDlg, int which) {
					iDlg.cancel();
				}
			};
		}
		alertDlg.setButton("确定", click);

		alertDlg.show();
		return alertDlg;
	}

	public static void dismissAlert(Context context) {
		Dialog dialog = (Dialog) dialogs.get(context.toString());
		if ((dialog != null) && (dialog.isShowing())) {
			dialog.dismiss();
			dialogs.remove(context.toString());
		}
	}

}
